package com.lanswon.qzsmk.controller.cardManageController;

import java.sql.Date;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.service.cardManageService.ApplyCardService;
import com.lanswon.qzsmk.service.cardManageService.MakeCardService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.vo.MakeCardQueryConditionVo;
import com.lanswon.qzsmk.vo.MakeCardQueryResultVo;
import com.lanswon.qzsmk.vo.PersonalVo;

/**
 * 制卡任务导出Controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="makeCardTaskExport")
public class MakeCardTaskExportController {
	
	@Autowired
	ApplyCardService acs;
	
	@Autowired
	ParamManageService pm;
	
	@Autowired
	MakeCardService mcs;
	
	/**
	 * 纯粹跳转连接用的方法(跳转制卡导出页面)
	 * @return
	 */
	@RequiresPermissions("makeCardTaskExport:index")
	@RequestMapping(value="/makeCardBatchExport",name="批量制卡导出,makeCardTaskExport:index",method=RequestMethod.GET)
	public String batchExport(){
		return "MakeCardBatchExport";
	}
	
	/**
	 * 根据任务编号获得个人信息，跳转任务生成页面中dialog
	 * @return
	 */
	@RequiresPermissions("makeCardTaskExport:infosByTasked")
	@RequestMapping(value="/infosByTasked",name="批量制卡导出任务编号获得个人信息,makeCardTaskExport:infosByTasked",method=RequestMethod.GET)
	public String ApplicationInfo( String parm,HttpServletRequest request){
		MakeCardQueryConditionVo mcv=new MakeCardQueryConditionVo();
		mcv.setCardTaskNo(parm);//存放任务编号
		mcv.setCurrentPage(1);
		mcv.setNumPerPage(20);
		mcv.setStartLine((mcv.getCurrentPage()-1)*mcv.getNumPerPage());
		System.out.println(mcv);
		DwzPage<PersonalVo> dPage=mcs.queryPersonalByCardTaskNo(mcv);
		request.getSession().setAttribute("dPage", dPage);
		return "ApplicationInfos";
	}
	
	/**
	 * 获取制卡任务信息，显示在页面中
	 * @return
	 * @throws ParseException 
	 */
	@RequiresPermissions("makeCardTaskExport:getTaskInfo")
	@RequestMapping(value="/getTaskInfo",name="批量制卡导出获取制卡任务信息,makeCardTaskExport:getTaskInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<MakeCardQueryResultVo> getTaskInfo(MakeCardQueryConditionVo mcv,Integer pageNum,Integer numPerPage) throws ParseException{
		System.out.println(mcv);
		DwzPage<MakeCardQueryResultVo> dPage=null;
//		if(mcv.getNumPerPage()==0){//页面加载
//			mcv.setCurrentPage(1);
//			mcv.setNumPerPage(20);
//			mcv.setTaskType("1");
//		}else{//页面分页
//			if(mcv.getCurrentPage()<=1||pageCount==0){
//				mcv.setCurrentPage(1);
//			}else if(mcv.getCurrentPage()>=pageCount&&pageCount!=0){
//				mcv.setCurrentPage(pageCount);
//			}
//			mcv.setTaskType("1");
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;			
		}
		mcv.setCurrentPage(pageNum);
		mcv.setNumPerPage(numPerPage);
		mcv.setTaskType("1");
		mcv.setStartLine((mcv.getCurrentPage()-1)*mcv.getNumPerPage());
		dPage=mcs.queryCardTask(mcv);
		System.out.println(dPage.getPagedList().size()+"条数据*********");
		return dPage;
	}
	
	/**
	 * 导出记名制卡任务
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("makeCardTaskExport:exportTask")
	@RequestMapping(value="/exportTask",name="批量制卡导出记名制卡任务,makeCardTaskExport:exportTask",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> exportTask(String[]array,HttpServletRequest request) throws Exception{
		System.out.println("导出制卡任务%……%%……%……%……%"+array.length);
		List<MakeCardQueryResultVo> list=new ArrayList<MakeCardQueryResultVo>();
		for(int i=0;i<array.length;i++){
			MakeCardQueryResultVo mcrvo=new MakeCardQueryResultVo();
			String item=array[i];
			mcrvo.setPropertiesByTasked(item.split("#"));
			System.out.println(mcrvo);
			list.add(mcrvo);
		}
		String root = request.getSession().getServletContext().getRealPath("/");
		root+="|"+request.getSession().getServletContext().getContextPath();

		List<String> paths =  mcs.exportMakeCardFile(root,list);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("statusCode", "200");
		map.put("message", "导出任务成功");
		map.put("paths", paths);
//		map.put("callbackType", "forward");
//		map.put("forwardUrl", "makeCardTaskExport/makeCardBatchExport");
		return map;
	}
	
}
